package com.sg.service.biz.component.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.component.req.*;
import com.sg.dto.biz.component.res.*;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class CalculationRulesServiceImpl implements com.wicket.okrcomponent.biz.service.CalculationRulesService {
    @Resource
    private RedisUtil redisUtil;
    //@Resource
    //private MOmsExecuteCycleStageService mOmsExecuteCycleStageService;
    @Resource
    private NbCalculationRules nbCalculationRules;
//@Resource
    //private MCustomFieldsService mCustomFieldsService;

    /**
     * D2-获取当前时间服务(公共)[3378]
     * gen by moon at 9/11/2023, 12:45:55 PM
     */
    @Trace(operationName = "D2-获取当前时间服务(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ObtainCurrentCycleServiceComRespDto obtainCurrentCycleServiceCom(ObtainCurrentCycleServiceComReqDto reqDto) {


        ObtainCurrentCycleRespDto getTargetCycleRes_1 = null;
//步骤0: M2获取当前周期 - obtainCurrentCycle
        //ModelCode: getTargetCycle
        ObtainCurrentCycleRespDto getTargetCycleRes = null;
        ObtainCurrentCycleReqDto getTargetCycleReq = new ObtainCurrentCycleReqDto();
        if (reqDto != null) {
            getTargetCycleReq.setSystemCurrentTime(reqDto.getSystemCurrentTime());//SimpleFieldAssign//sourceId:133928_1
            getTargetCycleReq.setSubcycleType(reqDto.getSubcycleType());//SimpleFieldAssign//sourceId:355791_1
        }
        if (reqDto != null && reqDto.getCurrentCycleList() != null && !CollectionUtil.isEmpty(reqDto.getCurrentCycleList())) {
            getTargetCycleReq.setCurrentCycleList(reqDto.getCurrentCycleList().stream().map(item -> BeanUtil.toBean(item, CurrentCycleDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:133931_1
        }
  
    /*M2获取当前周期ID以及下一周期ID[3132]  入参：当前时间，数据集【目标周期ID、周期ID、周期开始时间、周期结束时间】
逻辑：根据当前时间与数据集中数据开始时间、结束时间比较，获取当前目标周期ID以及下一个目标周期ID
出参：当前目标周期ID、下一个目标周期ID */
        Assert.isNull(getTargetCycleReq.getSystemCurrentTime(), "D2-获取当前时间服务(公共)-M2获取当前周期ID以及下一周期ID-系统当前时间不能为空", false);
        Assert.isNull(getTargetCycleReq.getSubcycleType(), "D2-获取当前时间服务(公共)-M2获取当前周期ID以及下一周期ID-子周期周期类型不能为空", false);
        getTargetCycleRes = nbCalculationRules.obtainCurrentCycle(getTargetCycleReq);


        getTargetCycleRes_1 = getTargetCycleRes;

        ObtainCurrentCycleServiceComRespDto retData = new ObtainCurrentCycleServiceComRespDto();
        if (getTargetCycleRes_1 != null) {
            retData.setCurrentCycleId(getTargetCycleRes_1.getCurrentCycleId());//SimpleFieldAssign//sourceId:133995_1
            retData.setNextCycleId(getTargetCycleRes_1.getNextCycleId());//SimpleFieldAssign//sourceId:133996_1
            retData.setCurrentSubCycleId(getTargetCycleRes_1.getCurrentSubCycleId());//SimpleFieldAssign//sourceId:355915_1
            retData.setNextSubCycleId(getTargetCycleRes_1.getNextSubCycleId());//SimpleFieldAssign//sourceId:355916_1
        }


        return retData;
    }

    /**
     * D2-计算时间偏移得最终时间服务(公共)[3407]
     * gen by moon at 10/17/2023, 12:21:58 PM
     */
    @Trace(operationName = "D2-计算时间偏移得最终时间服务(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public CalculateTimeMigrationServiceComRespDto calculateTimeMigrationServiceCom(CalculateTimeMigrationServiceComReqDto reqDto) {


        CalculateTimeMigrationRespDto timeMigrationRes_1 = null;
//步骤0: M2-计算时间偏移得最终时间 - calculateTimeMigration
        //ModelCode: timeMigration
        CalculateTimeMigrationRespDto timeMigrationRes = null;
        CalculateTimeMigrationReqDto timeMigrationReq = new CalculateTimeMigrationReqDto();
        if (reqDto != null) {
            timeMigrationReq.setCaluStartTime(reqDto.getCaluStartTime());//SimpleFieldAssign//sourceId:136487_1
            timeMigrationReq.setTimeDiffHours(reqDto.getTimeDiffHours() != null ? String.valueOf(reqDto.getTimeDiffHours()) : "");//SimpleFieldAssign//sourceId:136488_1
            timeMigrationReq.setTimeDiffDays(reqDto.getTimeDiffDays() != null ? String.valueOf(reqDto.getTimeDiffDays()) : "");//SimpleFieldAssign//sourceId:136489_1
        }

        /*M2-计算时间偏移得最终时间[3406]  入参：参照时间，偏移数（正值或负值），计算最终时间格式出参 */
        Assert.isNull(timeMigrationReq.getCaluStartTime(), "D2-计算时间偏移得最终时间服务(公共)-M2-计算时间偏移得最终时间-参照时间不能为空", false);
        timeMigrationRes = nbCalculationRules.calculateTimeMigration(timeMigrationReq);


        timeMigrationRes_1 = timeMigrationRes;

        CalculateTimeMigrationServiceComRespDto retData = new CalculateTimeMigrationServiceComRespDto();
        if (timeMigrationRes_1 != null) {
            retData.setCalculateMigrationTime(timeMigrationRes_1.getCalculateMigrationTime());//SimpleFieldAssign//sourceId:136494_1
        }


        return retData;
    }

    /**
     * D2-计算数据集求和(公共)[8083]
     * gen by moon at 12/5/2023, 12:05:44 AM
     */
    @Trace(operationName = "D2-计算数据集求和(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public CalculateSumOfMultipleNumComRespDto calculateSumOfMultipleNumCom(CalculateSumOfMultipleNumComReqDto reqDto) {


        AddSumOfMultipleNumServiceRespDto sumOfMultipleNumRes_1 = null;
//步骤0: M2-新增数据集求和服务（特殊方法） - addSumOfMultipleNumService
        //ModelCode: sumOfMultipleNum
        AddSumOfMultipleNumServiceRespDto sumOfMultipleNumRes = null;
        AddSumOfMultipleNumServiceReqDto sumOfMultipleNumReq = new AddSumOfMultipleNumServiceReqDto();
        if (reqDto != null && reqDto.getSumOfMultipleNumList() != null && !CollectionUtil.isEmpty(reqDto.getSumOfMultipleNumList())) {
            sumOfMultipleNumReq.setSumOfMultipleNumList(reqDto.getSumOfMultipleNumList().stream().map(item -> BeanUtil.toBean(item, SumOfMultipleNumDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1379463_1
        }
        if (reqDto != null) {
            sumOfMultipleNumReq.setDecimalPointRules(reqDto.getDecimalPointRules());//SimpleFieldAssign//sourceId:1379461_1
            sumOfMultipleNumReq.setDecimalDigitsLastRules(reqDto.getDecimalDigitsLastRules());//SimpleFieldAssign//sourceId:1379462_1
        }

        /*M2-新增数据集求和服务（特殊方法）[8082]  用户指标数据计算，入参数据集。将数据集的指定字段（数值字段）全部相加求和 */
        Assert.isNull(sumOfMultipleNumReq.getDecimalPointRules(), "D2-计算数据集求和(公共)-M2-新增数据集求和服务（特殊方法）-小数点规则不能为空", false);
        Assert.isNull(sumOfMultipleNumReq.getDecimalDigitsLastRules(), "D2-计算数据集求和(公共)-M2-新增数据集求和服务（特殊方法）-小数最后一位规则不能为空", false);
        sumOfMultipleNumRes = nbCalculationRules.addSumOfMultipleNumService(sumOfMultipleNumReq);


        sumOfMultipleNumRes_1 = sumOfMultipleNumRes;

        CalculateSumOfMultipleNumComRespDto retData = new CalculateSumOfMultipleNumComRespDto();
        if (sumOfMultipleNumRes_1 != null) {
            retData.setCalcResult(sumOfMultipleNumRes_1.getCalcResult());//SimpleFieldAssign//sourceId:1379479_1
        }


        return retData;
    }

    //
}
